package cn.yan.luowowo.advice;

import cn.yan.luowowo.exception.LogicException;
import cn.yan.luowowo.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 优化：
 * 统一异常处理
 */

@ControllerAdvice
@ResponseBody
public class CommonExceptionHandler {

    @ExceptionHandler(LogicException.class)

    public Object displayExp(Exception e, HttpServletResponse resp) throws IOException {

        e.printStackTrace();

        return new JsonResult(JsonResult.CODE_ERROR_PARAM,e.getMessage(),null);

    }

    @ExceptionHandler(RuntimeException.class)

    public Object runTimeExp(Exception e, HttpServletResponse resp) throws IOException {

        e.printStackTrace();

        return JsonResult.defaultError();

    }

}